package com.ebelter.nb.model.http.request;


import com.ebelter.btcomlib.models.beans.UpdateVersionBean;
import com.ebelter.btcomlib.models.https.MyStringCallback;
import com.ebelter.btcomlib.models.https.interfaces.HttpResponse;
import com.ebelter.btcomlib.utils.MD5Utils;
import com.ebelter.btcomlib.utils.log.LogUtils;
import com.ebelter.nb.beans.GetBC;
import com.ebelter.nb.beans.GetBP;
import com.ebelter.nb.model.http.HttpResponse2;
import com.ebelter.nb.model.http.MyStringCallback2;
import com.ebelter.nb.model.http.response.BandDeviceListResponse;
import com.ebelter.nb.model.http.response.Login_R;
import com.ebelter.nb.model.http.response.NbBandDeviceResponse;
import com.ebelter.nb.model.http.response.NbBaseResponse;
import com.ebelter.nb.utils.NbUtitls;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;

import okhttp3.Call;
import okhttp3.Response;

public class NbNetUtils {
    public static final String TAG = "NbNetUtils";

    private static final String NBURL = "www.baidu.com";

    private NbJsonUtils mNbJsonUtils;
    private static NbNetUtils instance;
    public NbNetUtils() {
        this.mNbJsonUtils = new NbJsonUtils();
    }
    public static NbNetUtils getInstance() {
        if (instance==null){
            synchronized (NbUtitls.class){
                instance = new NbNetUtils();
            }
        }
        return instance;
    }

    /**
     * postJson请求获取数据
     */
    private void postJson(Object tag, String url, String requestJson, StringCallback callback){
        OkGo.post(url).tag(tag).upJson(requestJson).execute(callback);
    }

    private void getRequest(Object tag, String url,StringCallback callback){
        OkGo.get(url).tag(tag).execute(callback);
    }
    private void postJsonNb(Object tag, String requestJson, StringCallback callback){
        OkGo.post(NBURL).tag(tag).upJson(requestJson).execute(callback);
    }

    public void cancelTag(Object tag){
        OkGo.getInstance().cancelTag(tag);
    }

    //-------------------------------------------------------------------------------------


    //private static final String NB_BASE_URL ="http://218.17.185.14:40022/sys/json/dataPost";//正式地址
    //private static final String NB_BASE_URL ="http://192.168.1.152:8282/bhcp/sys/json/dataPost";//测试地址
    //private static final String NB_BASE_URL ="http://192.168.1.152:8282/sys/json/dataPost";//测试地址
   // private static final String NB_BASE_URL ="http://119.23.18.73:8087/bhcp/sys/json/dataPost";//正式地址 尚珣

    private static final String NB_BASE_URL ="http://admin.ebelter.com/sys/json/dataPost";//正式地址 贺世诞
    private static final String NB_BASE_URL2 ="http://shangxun.belter.com.cn";//正式地址 李露

//    private static final String NB_BASE_URL ="http://nb.ebelter.com/sys/json/dataPost";//测试地址 李露
//    private static final String NB_BASE_URL2 ="http://nb.ebelter.com";//测试地址 李露



    //登录
    //  "resultCode": 0, "resultMessage": "登录失败"
    //  "resultCode": 2, "resultMessage": "账号或者密码错误"
    //  "resultCode": 3, "resultMessage": "当前手机号未注册"
    //  "resultCode": 1, "resultMessage": "登录成功"
    public void login(Object tag,String username,String psw,final HttpResponse2<Login_R> httpResponse){
        String json = mNbJsonUtils.getLoginJs(username,psw);
        postJson(tag,NB_BASE_URL,json,new MyStringCallback2<>(httpResponse,Login_R.class));
    }


    /**
     * @param phone 必须有
     * @param name
     * @param sn
     * @param sex 0男1女
     * @param height
     * @param profession 2 职业 2-非运动员，
     * @param birthday "1994-01-01",
     * @param dtype  设备类型 必须有
     * @param pwd
     * @param httpResponse
     *     "resultCode": 1,"resultMessage": "用户注册成功！"
     *     "resultCode": 0,"resultMessage": "用户注册设备失败"
            "resultCode": 102,"resultMessage": "设备未绑定组织"
            "resultCode": 101,"resultMessage": "该号码已注册"
     */
    public void register(Object tag,String phone,String name,String sn,
                         String sex,String height,String weight,int profession,String birthday,
                         String dtype,String pwd,String checkCode,final HttpResponse2<NbBaseResponse> httpResponse){
        String json = mNbJsonUtils.getRegisterJs(phone,name,sn,sex,height,weight,profession,birthday,dtype,pwd,checkCode);
        postJson(tag,NB_BASE_URL,json,new MyStringCallback2<>(httpResponse,NbBaseResponse.class));
    }

    public void register2(Object tag,String phone,String pwd,String checkCode,final HttpResponse2<NbBaseResponse> httpResponse){

        String json = "{\n" +
                "\t\"bizId\": \"01\",\n" +
                "\t\"funcId\": \"05\",\n" +
                "\t\"version\": \"07\",\n" +
                "\t\"authKey\": \"\",\n" +
                "\t\"data\": {\n" +
                "\t\t\"pwd\": \"123123\",\n" +
                "\t\t\"phone\": \"13510632160\",\n" +
                "\t\t\"dtype\": \"09\",\n" +
                "        \"checkCode\":\"3360\"\n" +
                "\t}\n" +
                "}";

        json=json.replace("123123",pwd);
        json=json.replace("13510632160",phone);
        json=json.replace("3360",checkCode);
        postJson(tag,NB_BASE_URL,json,new MyStringCallback2<>(httpResponse,NbBaseResponse.class));
    }



    public void requestPhoneCode(Object tag,String phone,final HttpResponse2<NbBaseResponse> httpResponse){
        String json = mNbJsonUtils.getrequestPhoneCodeJs(phone);
        postJson(tag,NB_BASE_URL,json,new MyStringCallback2<>(httpResponse,NbBaseResponse.class));
    }



    public void modifyPsw(Object tag,String phone,String psw,String checkCode,final HttpResponse2<NbBaseResponse> httpResponse){
        String json = mNbJsonUtils.getModifyPswJs(phone,psw,checkCode);
        postJson(tag,NB_BASE_URL,json,new MyStringCallback2<>(httpResponse,NbBaseResponse.class));
    }


    /**
     *
     * @param tag
     * @param authKey
     * @param identification 设备标识 按键1 按键2
     * @param ano 是否有心率不齐
     * @param deviceId 设备ID
     * @param time
     * @param systolic
     * @param diastolic
     * @param pulse
     * @param httpResponse
     */
    public void uploadBpmData(Object tag,String authKey,String userType,int identification,int ano,String deviceId,
                              String time,int systolic,int diastolic,int pulse,final HttpResponse2<NbBaseResponse> httpResponse){{
            String json = mNbJsonUtils.getUploadBpmJs(authKey,userType,identification,ano,deviceId,time,systolic,diastolic,pulse);
            postJson(tag,NB_BASE_URL,json,new MyStringCallback2<>(httpResponse,NbBaseResponse.class));
        }
    }


    public void uploadScaleData(Object tag,String authKey,String deviceId,String time,float muscle,float adiposerate,
                                int visceralfat,float moisture, float bone,float thermal, float basalMetabolism ,
                                float metabolism,float bmi,float impedance,float weight,float protein,int hart,
                                float skeletalMuscle,int physicalAge,int bodyScore,
                                final HttpResponse2<NbBaseResponse> httpResponse){
        String json = mNbJsonUtils.getUploadScaleJs(authKey,deviceId,time,muscle,time,adiposerate,visceralfat,moisture,bone,thermal,
                basalMetabolism,metabolism,bmi,impedance,weight,protein,hart,skeletalMuscle,physicalAge,bodyScore);
        postJson(tag,NB_BASE_URL,json,new MyStringCallback2<>(httpResponse,NbBaseResponse.class));
    }


    /**
     * @param dateType 是	日期类型 1:年 2:月 3周: 4:天
     * @param param 年：2018  月2018-01 周 2018-01-01（转开始日期就行，后台自动T+7） 天 2018-01-02
     */
    public void getBpmTrendDatas(Object tag,String authKey,int dateType,String param,final HttpResponse2<NbBaseResponse> httpResponse){
        String json = mNbJsonUtils.getBpmTrendDatasJs(authKey,dateType,param);
        postJson(tag,NB_BASE_URL,json,new MyStringCallback2<>(httpResponse,NbBaseResponse.class));
    }



    //用户绑定设备接口
    //结果说明 0---操作成功---1SN号码未绑定IEMI---2你已经绑定过一台设备,请先解除绑定！
    public void userBandDevice(Object tag, String sn_id, String user_id, int type, final HttpResponse<NbBandDeviceResponse> httpResponse){
        //http://shangxun.belter.com.cn/service-api/webiot/userBandDevice?sn_id=N01SN00000001&user_id=0000851B889D472AAD605846E68140A0&type=0
        String url = NB_BASE_URL2+"/service-api/webiot/userBandDevice?sn_id="+sn_id+"&user_id="+user_id+"&type="+type;
        getRequest(tag,url,new MyStringCallback<>(httpResponse,NbBandDeviceResponse.class));
    }

    //用户解除绑定设备列表接口
    //{"msg":"操作成功","status":0}
    public void userUnbandDevice(Object tag,String id, final HttpResponse<NbBandDeviceResponse> httpResponse){
        String url =NB_BASE_URL2+"/service-api/webiot/unbindDeviceInfo?id="+id;
        getRequest(tag,url,new MyStringCallback<>(httpResponse,NbBandDeviceResponse.class));
    }


    //获取用户绑定设备接口
    public void getUserBandList(Object tag,String user_id,final HttpResponse<BandDeviceListResponse> httpResponse){
        //http://shangxun.belter.com.cn/service-api/webiot/getUserBandDeviceInfo?user_id=0000851B889D472AAD605846E68140A0
        String url=NB_BASE_URL2+"/service-api/webiot/getUserBandDeviceInfo?user_id="+user_id;
        getRequest(tag,url,new MyStringCallback<>(httpResponse, BandDeviceListResponse.class));
    }


    //更新用户信息
    public void updateUserInfo(Object tag,String authKey,String name,int sex,String birthday,int height,float weight,
                               int profession,String dtype,final HttpResponse2<NbBaseResponse> httpResponse){
        String json = mNbJsonUtils.getUpdateUserInfo(authKey,name,sex,birthday,height,weight,profession,dtype);
        postJson(tag,NB_BASE_URL,json,new MyStringCallback2<>(httpResponse,NbBaseResponse.class));

    }


    //获取用户绑定设备接口
    public void sendUserInfo2Scale(Object tag,String user_id,String imei,int age,int height,float weight,int sex,int profession,final HttpResponse<BandDeviceListResponse> httpResponse){
        //http://shangxun.belter.com.cn/service-api/webiot/getUserBandDeviceInfo?user_id=0000851B889D472AAD605846E68140A0
        String url=NB_BASE_URL2+"/service-api/webiot/pushUserInfoCommand?imei_number="+imei+"&user_id="+user_id+"&user_age="+age+"&user_height="+height+"&user_weight="+weight+"&user_sex="+sex+"&user_type="+profession;
        getRequest(tag,url,new MyStringCallback<>(httpResponse, BandDeviceListResponse.class));
    }


    /**
     * @param dateType  1:年 2:月 3周: 4:天
     * @param param 参数值 年：2018  月2018-01 周 2018-01-01（转开始日期就行，后台自动T+7） 天 2018-01-02
     * @param userType 区分用户1/2
     * @param measureType 1血压 2体成分
     */
    public void  getTrendData(Object tag,String authKey,
                              int dateType,
                              String param,
                              String userType,
                              String measureType,
                              final HttpResponse2<NbBaseResponse> httpResponse){
        String json = mNbJsonUtils.getTrendData(authKey,dateType,param,userType,measureType);
        LogUtils.i(TAG,"---getTrendData--json = " + json);
        postJson(tag,NB_BASE_URL,json,new MyStringCallback2<>(httpResponse,NbBaseResponse.class));
    }


    //获取血压列表
    public void getBloodPressurePage(Object tag,String authKey,int pageNo,int pageSize,String userType, final HttpResponse<GetBP> httpResponse){
        String json = mNbJsonUtils.getBloodPressurePageJS(authKey,pageNo,pageSize,userType);
        postJson(tag,NB_BASE_URL,json,new MyStringCallback<>(httpResponse,GetBP.class));
    }

    //获取人体成分列表
    public void getBodyCompositionPage(Object tag,String authKey,int pageNo,int pageSize,String userType,final HttpResponse<GetBC> httpResponse){
        String json = mNbJsonUtils.getBodyCompositionPageJS(authKey,pageNo,pageSize,userType);
        postJson(tag,NB_BASE_URL,json,new MyStringCallback<>(httpResponse,GetBC.class));
    }

    public void getUpdateVersionJson(Object tag, final HttpResponse<UpdateVersionBean> httpResponse){
        OkGo.post("http://o.ebelter.com/apk_update/nb_apk/version.json").tag(tag).execute(new MyStringCallback<>(httpResponse,UpdateVersionBean.class));
    }
}
